home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / rules / prs2 / prs2explain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-05  |  5.0 KB  |  199 lines

  1. /*---------------------------------------------------------------------
  2.  *
  3.  * IDENTIFICATION:
  4.  * $Header: /private/postgres/src/rules/prs2/RCS/prs2explain.c,v 1.7 1991/11/18 22:21:22 mer Exp $
  5.  *
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <strings.h>
  10.  
  11. #include "tmp/postgres.h"
  12.  
  13. RcsId("$Header: /private/postgres/src/rules/prs2/RCS/prs2explain.c,v 1.7 1991/11/18 22:21:22 mer Exp $");
  14.  
  15. #include "access/heapam.h"
  16. #include "access/tupdesc.h"
  17. #include "access/ftup.h"
  18. #include "utils/log.h"
  19. #include "catalog/syscache.h"
  20. #include "storage/smgr.h"
  21.  
  22. /*---------------------------------------------------------------------
  23.  *
  24.  * createExplainRelation
  25.  *
  26.  * create the relation wehre the output of the explain command
  27.  * will be stored.
  28.  *
  29.  * XXX: If the relation exists, then we destroy it and create a
  30.  * new one. We might want to change that....
  31.  */
  32.  
  33. Relation
  34. createExplainRelation(relationName) 
  35. Name relationName;
  36. {
  37.     ArchiveMode archiveMode;
  38.     AttributeNumber numberOfAttributes;
  39.     TupleDesc tupleDesc;
  40.     ObjectId relationOid;
  41.     NameData attrName;
  42.     NameData typeName;
  43.     Relation reldesc;
  44.     HeapTuple tuple;
  45.     Relation relDesc;
  46.  
  47.     /*
  48.      * check the arguments for stupid values...
  49.      */
  50.     if (!NameIsValid(relationName)) {
  51.     elog(WARN, "Illegal relation name %s", relationName->data);
  52.     }
  53.  
  54.     /*
  55.      * If the relation exists, destroy it
  56.      */
  57.     tuple = SearchSysCacheTuple(RELNAME, relationName);
  58.     if (HeapTupleIsValid(tuple)) {
  59.     RelationNameDestroyHeapRelation((char *)relationName);
  60.     }
  61.  
  62.     /*
  63.      * Create the new relation schema
  64.      */
  65.     archiveMode = 'n';
  66.     numberOfAttributes = 4;
  67.  
  68.     tupleDesc = CreateTemplateTupleDesc(numberOfAttributes);
  69.  
  70.     strcpy(attrName.data, "rulename");
  71.     strcpy(typeName.data, "char16");
  72.     TupleDescInitEntry(tupleDesc,1, &attrName, &typeName, 0);
  73.  
  74.     strcpy(attrName.data, "depth");
  75.     strcpy(typeName.data, "int4");
  76.     TupleDescInitEntry(tupleDesc,2, &attrName, &typeName, 0);
  77.  
  78.     strcpy(attrName.data, "tupleoid");
  79.     strcpy(typeName.data, "oid");
  80.     TupleDescInitEntry(tupleDesc,3, &attrName, &typeName, 0);
  81.     
  82.     strcpy(attrName.data, "relname");
  83.     strcpy(typeName.data, "char16");
  84.     TupleDescInitEntry(tupleDesc,4, &attrName, &typeName, 0);
  85.  
  86.     /*
  87.      * Create the relation.
  88.      * NOTE: `TupleDescriptorData' is the same thing
  89.      * as a `TupleDesc'.
  90.      */
  91.     relationOid = RelationNameCreateHeapRelation(
  92.             (char *)relationName,
  93.             archiveMode,
  94.             numberOfAttributes,
  95.             DEFAULT_SMGR,
  96.             (struct attribute **)tupleDesc);
  97.  
  98.     if (!ObjectIdIsValid(relationOid)) {
  99.     elog(WARN,"Can not create relation `%s'.", relationName->data);
  100.     }
  101.  
  102.     /*
  103.      * OK, now open the relation.
  104.      * XXX: what does this `setheapoverride' do ??
  105.      *      (everybody is using it....)
  106.      */
  107.     setheapoverride(true);
  108.     relDesc = ObjectIdOpenHeapRelation(relationOid);
  109.     setheapoverride(false);
  110.     if (!RelationIsValid(relDesc)) {
  111.     elog(WARN,"Can not open created relation `%s'.", relationName->data);
  112.     }
  113.  
  114.     return(relDesc);
  115. }
  116.  
  117. /*--------------------------------------------------------------------
  118.  *
  119.  * storeExplainInfo
  120.  *
  121.  * Append a tuple to the 'explain' relation
  122.  */
  123. void
  124. storeExplainInfo(explainRelation, ruleId, relation, tupleOid)
  125. Relation explainRelation;
  126. ObjectId ruleId;
  127. Relation relation;
  128. ObjectId tupleOid;
  129. {
  130.     NameData relName;
  131.     NameData ruleName;
  132.     HeapTuple explainTuple;
  133.     TupleDescriptor explainTupleDesc;
  134.     Datum data[4];
  135.     char null[4];
  136.     int i;
  137.  
  138.  
  139.     /*
  140.      * check the arguments....
  141.      */
  142.     if (!RelationIsValid(explainRelation)) {
  143.     elog(WARN, "storeExplainInfo: ivnalid explain relation");
  144.     }
  145.     if (!RelationIsValid(relation)) {
  146.     elog(WARN, "storeExplainInfo: ivnalid scan relation");
  147.     }
  148.     if (!ObjectIdIsValid(ruleId)) {
  149.     elog(WARN, "storeExplainInfo: invalid rule Id");
  150.     }
  151.     if (!ObjectIdIsValid(tupleOid)) {
  152.     elog(WARN, "storeExplainInfo: invalid tuple oid");
  153.     }
  154.  
  155.     /*
  156.      * find the rule's name
  157.      */
  158.     strcpy(ruleName.data, "No-Name");
  159.  
  160.     /*
  161.      * find the scan relation's name
  162.      */
  163.    /*
  164.     * kai: It's a wonder, that the following compiles on some machines:
  165.     * RelationGetRelationTupleForm(relation)->relname is of type char16, and
  166.     * char16 is a structure with a 16 character string as only field:
  167.     *
  168.     * strncpy(relName.data,
  169.     *     RelationGetRelationTupleForm(relation)->relname, 16);
  170.     */
  171.     strncpy(relName.data,
  172.     &RelationGetRelationTupleForm(relation)->relname, 16);
  173.     
  174.     
  175.     /*
  176.      * Now form the tuple that will be stored in the
  177.      * `explain' relation.
  178.      */
  179.     for (i=0; i<4; i++) {
  180.     null[i] = ' ';
  181.     }
  182.     data[0] = NameGetDatum(&ruleName);
  183.     data[1] = Int32GetDatum(0); /* XXXX THIS IS THE RECURSION DEPTH */
  184.     data[2] = ObjectIdGetDatum(tupleOid);
  185.     data[3] = NameGetDatum(&relName);
  186.  
  187.     explainTupleDesc = RelationGetTupleDescriptor(explainRelation);
  188.     explainTuple = FormHeapTuple((AttributeNumber) 4,
  189.                     explainTupleDesc,
  190.                     data,
  191.                     null);
  192.     
  193.     /*
  194.      * Insert the tuple
  195.      */
  196.     (void) RelationInsertHeapTuple(explainRelation, explainTuple,
  197.                     (double *) NULL);
  198. }
  199.